package com.a3dweijing.wetouch2.fragment;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.VideoView;

import com.a3dweijing.wetouch2.Activity.MainActivity;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.datasave.Singleton;

/**
 * Created by Yxd on 2018/3/21.
 */

public class VideoPlayFragment extends Fragment {
    static VideoPlayFragment inctance;
    MainActivity activity;
    private ProgressBar progressBar;
    private VideoView videoView;
    private ImageView close;
    private String url1 = "http://flashmedia.eastday.com/newdate/news/2016-11/shznews1125-19.mp4";
    private String url2 = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov";
    private String url3 = "http://42.96.249.166/live/388.m3u8";
    private String url4 = "http://61.129.89.191/ThroughTrain/download.html?id=4035&flag=-org-";
    int sw, sh;
    boolean frist = true;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (MainActivity) activity;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_play, container, false);
        videoView = view.findViewById(R.id.frame_video);
        progressBar = view.findViewById(R.id.loading);
        videoloud();
        close = view.findViewById(R.id.close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                videoloud();
                if (videoView.isPlaying()) {
                    videoView.pause();
                    videoView.stopPlayback();
                }
            }
        });
        loadView(getArguments().getString("url"));
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }

    public void loadView(String path) {
        Uri uri = Uri.parse(path);
        videoView.setVideoPath(path);
        videoView.start();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                //         mp.setLooping(true);
                mp.start();// 播放
                sh = mp.getVideoHeight();
                sw = mp.getVideoWidth();
//                Toast.makeText(getContext(), "开始播放！", Toast.LENGTH_LONG).show();
                progressBar.setVisibility(View.GONE);


            }
        });

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
//                Toast.makeText(getContext(), "播放完毕", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void videoloud() {

        int i = getResources().getConfiguration().orientation;
        if (i == Configuration.ORIENTATION_PORTRAIT) {
            activity.getSupportActionBar().hide();
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            ViewGroup.LayoutParams lp = videoView.getLayoutParams();
            lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
            lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
            videoView.setLayoutParams(lp);
        } else {
            activity.setFrameHide();
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            ViewGroup.LayoutParams lp = videoView.getLayoutParams();
            lp.width = sw;
            lp.height = sh;
            Log.e("--", "onClick: " + sw + ":" + sh);
            videoView.setLayoutParams(lp);
        }
    }

    public static VideoPlayFragment getInctance(String url) {
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        Log.e("--", "getInctance: " + url);
        inctance = new VideoPlayFragment();
        inctance.setArguments(bundle);
        return inctance;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (videoView.isPlaying()) {
            videoView.pause();
            videoView.stopPlayback();
        }
    }
}
